package com.markerhub.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.*;
import com.markerhub.service.SysCourseApplyService;
import com.markerhub.service.SysCourseService;
import com.markerhub.utils.CSVUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.baomidou.mybatisplus.core.toolkit.IdWorker.getId;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.lang.reflect.Array;
import java.util.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wang
 * @since 2022-04-07
 */
@RestController
@RequestMapping("/sys/course/apply")
public class SysCourseApplyController extends BaseController {
    @Autowired
    SysCourseApplyService sysCourseApplyService;//注入service

    @Autowired
    SysCourseService sysCourseService;//注入service


    @PostMapping("/single")//填充表单的接口
    public Result single(@RequestBody SysCourseApply sysCourseApply){

        sysCourseApply=sysCourseApplyService.getById(sysCourseApply.getId());

        return Result.succ(sysCourseApply);
    }


//    @PostMapping("/update")//教师修改课程信息申请接口
//    public Result update(@RequestBody SysCourse sysCourse){
//        SysCourseApply sysCourseApply = new SysCourseApply();
//        sysCourseApply.setCourseId(sysCourse.getCourseId());
//        sysCourseApply.setCourseName(sysCourse.getCourseName());
//        sysCourseApply.setCredit(sysCourse.getCredit());
//        sysCourseApply.setHour(sysCourse.getHour());
//        sysCourseApply.setIntro(sysCourse.getIntro());
//        sysCourseApply.setLocation(sysCourse.getLocation());
//        sysCourseApply.setMajor(sysCourse.getMajor());
//        sysCourseApply.setSchool(sysCourse.getSchool());
//        sysCourseApply.setTeacher(sysCourse.getTeacher());
//        sysCourseApply.setTeacherId(sysCourse.getTeacherId());
//        sysCourseApply.setVolume(sysCourse.getVolume());
//        sysCourseApply.setStatu(3);
//        sysCourseApply.setTarget(Math.toIntExact(sysCourse.getId()));
//        sysCourseApply.setLocation(sysCourse.getLocation());
//        sysCourseApply.setTime(sysCourse.getTimeId());
//        sysCourseApplyService.save(sysCourseApply);
//        return Result.succ("");
//    }

    @GetMapping("/update/list")//管理员得到待审核的修改申请
    public Result updateList(){
        List<SysCourseApply> pageData = sysCourseApplyService.list(new QueryWrapper<SysCourseApply>()
                .eq("statu", 3));
        pageData.forEach(u ->{
            String[] tep= u.getTime().split(",");
            List<String> s=Arrays.asList(tep);
            String time ="";
            Vector<Long> weekday = new Vector<Long>();
            Integer total= sysTimeService.getOne(new QueryWrapper<SysTime>().orderByDesc("timespan").last("limit 1")).getTimespan();
            for (int i =0;i<s.size();i++){
                Long temp = Long.valueOf(s.get(i));
                if(temp%total==0){
                    if(weekday.contains(temp/total)){
                        time+=",第"+total+"节";
                    }
                    else{
                        if(weekday.size()==0){
                            time+= "周" + (temp / total) + "第" + total+"节";
                            weekday.add(temp/total);
                        }
                        else{
                            time+= ";周" + (temp / total) + "第" + total+"节";
                            weekday.add(temp/total);
                        }
                    }
                    continue;
                }
                int j;
                for (j=0;j<weekday.size();j++){
                    if((temp/total+1)==weekday.get(j)){
                        break;
                    }
                }
                if(j==weekday.size()){
                    weekday.add(temp/total+1);
                    if(weekday.size()==1) {
                        time += "周" + (1+temp / total) + "第" + temp % total+"节";
                    }
                    else{
                        time+= ";周" + (1+temp / total) + "第" + temp % total+"节";
                    }
                }
                else{
                    time+=",第"+temp%total+"节";
                }

            }
            u.setTime(time);

        });
        return Result.succ(pageData);
    }

//    @PostMapping("/delete")//教师删除课程信息申请接口,教师申请删除
//    public Result delete(@RequestBody SysCourse sysCourse){
//
//        SysCourseApply sysCourseApply=new SysCourseApply();
//        sysCourseApply.setCourseId(sysCourse.getCourseId());
//        sysCourseApply.setCourseName(sysCourse.getCourseName());
//        sysCourseApply.setTeacher(sysCourse.getTeacher());
//        sysCourseApply.setTeacherId(sysCourse.getTeacherId());
//        sysCourseApply.setTarget(Math.toIntExact(sysCourse.getId()));
//        sysCourseApply.setStatu(2);
//        sysCourseApplyService.save(sysCourseApply);
//        return Result.succ("");
//    }

    @GetMapping("/delete/list")//管理员得到待审核的删除申请
    public Result deleteList() {
        List<SysCourseApply> pageData = sysCourseApplyService.list(new QueryWrapper<SysCourseApply>()
                .eq("statu", 2));
        pageData.forEach(u ->{
            String[] tep= u.getTime().split(",");
            List<String> s=Arrays.asList(tep);
            String time ="";
            Vector<Long> weekday = new Vector<Long>();
            Integer total= sysTimeService.getOne(new QueryWrapper<SysTime>().orderByDesc("timespan").last("limit 1")).getTimespan();
            for (int i =0;i<s.size();i++){
                Long temp = Long.valueOf(s.get(i));
                if(temp%total==0){
                    if(weekday.contains(temp/total)){
                        time+=",第"+total+"节";
                    }
                    else{
                        if(weekday.size()==0){
                            time+= "周" + (temp / total) + "第" + total+"节";
                            weekday.add(temp/total);
                        }
                        else{
                            time+= ";周" + (temp / total) + "第" + total+"节";
                            weekday.add(temp/total);
                        }
                    }
                    continue;
                }
                int j;
                for (j=0;j<weekday.size();j++){
                    if((temp/total+1)==weekday.get(j)){
                        break;
                    }
                }
                if(j==weekday.size()){
                    weekday.add(temp/total+1);
                    if(weekday.size()==1) {
                        time += "周" + (1+temp / total) + "第" + temp % total+"节";
                    }
                    else{
                        time+= ";周" + (1+temp / total) + "第" + temp % total+"节";
                    }
                }
                else{
                    time+=",第"+temp%total+"节";
                }

            }
            u.setTime(time);

        });
        return Result.succ(pageData);
    }


//    @PostMapping("/save")
//    public Result save(@RequestBody SysCourse sysCourse){
//
//        SysCourseApply sysCourseApply = new SysCourseApply();
//        sysCourseApply.setCourseId(sysCourse.getCourseId());
//        sysCourseApply.setCourseName(sysCourse.getCourseName());
//        sysCourseApply.setCredit(sysCourse.getCredit());
//        sysCourseApply.setHour(sysCourse.getHour());
//        sysCourseApply.setIntro(sysCourse.getIntro());
//        sysCourseApply.setLocation(sysCourse.getLocation());
//        sysCourseApply.setMajor(sysCourse.getMajor());
//        sysCourseApply.setSchool(sysCourse.getSchool());
//        sysCourseApply.setTeacher(sysCourse.getTeacher());
//        sysCourseApply.setTeacherId(sysCourse.getTeacherId());
//        sysCourseApply.setVolume(sysCourse.getVolume());
//        sysCourseApply.setStatu(1);
//        sysCourseApply.setLocation(sysCourse.getLocation());
//        sysCourseApply.setTime(sysCourse.getTimeId());
//        sysCourseApplyService.save(sysCourseApply);
//        return Result.succ("");
//    }

    @GetMapping("/save/list")//展示新增信息的审核接口
    public Result saveList(){
        List<SysCourseApply> pageData = sysCourseApplyService.list( new QueryWrapper<SysCourseApply>()
                .eq("statu", 1));
        pageData.forEach(u ->{
            String[] tep= u.getTime().split(",");
            List<String> s=Arrays.asList(tep);
            String time ="";
            Vector<Long> weekday = new Vector<Long>();
            Integer total= sysTimeService.getOne(new QueryWrapper<SysTime>().orderByDesc("timespan").last("limit 1")).getTimespan();
            for (int i =0;i<s.size();i++){
                Long temp = Long.valueOf(s.get(i));
                if(temp%total==0){
                    if(weekday.contains(temp/total)){
                        time+=",第"+total+"节";
                    }
                    else{
                        if(weekday.size()==0){
                            time+= "周" + (temp / total) + "第" + total+"节";
                            weekday.add(temp/total);
                        }
                        else{
                            time+= ";周" + (temp / total) + "第" + total+"节";
                            weekday.add(temp/total);
                        }
                    }
                    continue;
                }
                int j;
                for (j=0;j<weekday.size();j++){
                    if((temp/total+1)==weekday.get(j)){
                        break;
                    }
                }
                if(j==weekday.size()){
                    weekday.add(temp/total+1);
                    if(weekday.size()==1) {
                        time += "周" + (1+temp / total) + "第" + temp % total+"节";
                    }
                    else{
                        time+= ";周" + (1+temp / total) + "第" + temp % total+"节";
                    }
                }
                else{
                    time+=",第"+temp%total+"节";
                }

            }
            u.setTime(time);

        });
        return Result.succ(pageData);
    }

    @PostMapping("/deletelist")
    public Result deletelist(HttpServletRequest request) {
        if (sysCourseApplyService.removeById(Long.valueOf(request.getParameter("id"))))
        return Result.succ("remove successful!");
        else
            return Result.fail("id not find");
    }

    @PostMapping("save/list/teacher")//展示新增信息的审核接口
    public Result saveListTeacher(@RequestBody SysCourse sysCourse){

        Map<String, Object> params = new HashMap<>();
        params.put("teacher_id",sysCourse.getTeacherId());
        params.put("statu",1);

        List<SysCourseApply> pageData = sysCourseApplyService.list( new QueryWrapper<SysCourseApply>()
                .allEq(params));
        pageData.forEach(u ->{
            String[] tep= u.getTime().split(",");
            List<String> s=Arrays.asList(tep);
            String time ="";
            Vector<Long> weekday = new Vector<Long>();
            Integer total= sysTimeService.getOne(new QueryWrapper<SysTime>().orderByDesc("timespan").last("limit 1")).getTimespan();
            for (int i =0;i<s.size();i++){
                Long temp = Long.valueOf(s.get(i));
                if(temp%total==0){
                    if(weekday.contains(temp/total)){
                        time+=",第"+total+"节";
                    }
                    else{
                        if(weekday.size()==0){
                            time+= "周" + (temp / total) + "第" + total+"节";
                            weekday.add(temp/total);
                        }
                        else{
                            time+= ";周" + (temp / total) + "第" + total+"节";
                            weekday.add(temp/total);
                        }
                    }
                    continue;
                }
                int j;
                for (j=0;j<weekday.size();j++){
                    if((temp/total+1)==weekday.get(j)){
                        break;
                    }
                }
                if(j==weekday.size()){
                    weekday.add(temp/total+1);
                    if(weekday.size()==1) {
                        time += "周" + (1+temp / total) + "第" + temp % total+"节";
                    }
                    else{
                        time+= ";周" + (1+temp / total) + "第" + temp % total+"节";
                    }
                }
                else{
                    time+=",第"+temp%total+"节";
                }

            }
            u.setTime(time);

        });
        return Result.succ(pageData);
    }

    @PostMapping("update/list/teacher")//展示新增信息的审核接口
    public Result updateListTeacher(@RequestBody SysCourse sysCourse){

        Map<String, Object> params = new HashMap<>();
        params.put("teacher_id",sysCourse.getTeacherId());
        params.put("statu",3);


        List<SysCourseApply> pageData = sysCourseApplyService.list(new QueryWrapper<SysCourseApply>()
                .allEq(params));
        pageData.forEach(u ->{
            String[] tep= u.getTime().split(",");
            List<String> s=Arrays.asList(tep);
            String time ="";
            Vector<Long> weekday = new Vector<Long>();
            Integer total= sysTimeService.getOne(new QueryWrapper<SysTime>().orderByDesc("timespan").last("limit 1")).getTimespan();
            for (int i =0;i<s.size();i++){
                Long temp = Long.valueOf(s.get(i));
                if(temp%total==0){
                    if(weekday.contains(temp/total)){
                        time+=",第"+total+"节";
                    }
                    else{
                        if(weekday.size()==0){
                            time+= "周" + (temp / total) + "第" + total+"节";
                            weekday.add(temp/total);
                        }
                        else{
                            time+= ";周" + (temp / total) + "第" + total+"节";
                            weekday.add(temp/total);
                        }
                    }
                    continue;
                }
                int j;
                for (j=0;j<weekday.size();j++){
                    if((temp/total+1)==weekday.get(j)){
                        break;
                    }
                }
                if(j==weekday.size()){
                    weekday.add(temp/total+1);
                    if(weekday.size()==1) {
                        time += "周" + (1+temp / total) + "第" + temp % total+"节";
                    }
                    else{
                        time+= ";周" + (1+temp / total) + "第" + temp % total+"节";
                    }
                }
                else{
                    time+=",第"+temp%total+"节";
                }

            }
            u.setTime(time);

        });
        return Result.succ(pageData);
    }

    @PostMapping("delete/list/teacher")//展示新增信息的审核接口
    public Result deleteListTeacher(@RequestBody SysCourse sysCourse){

        Map<String, Object> params = new HashMap<>();
        params.put("teacher_id",sysCourse.getTeacherId());
        params.put("statu",2);


        List<SysCourseApply> pageData = sysCourseApplyService.list(new QueryWrapper<SysCourseApply>()
                .allEq(params));
        pageData.forEach(u ->{
            String[] tep= u.getTime().split(",");
            List<String> s=Arrays.asList(tep);
            String time ="";
            Vector<Long> weekday = new Vector<Long>();
            Integer total= sysTimeService.getOne(new QueryWrapper<SysTime>().orderByDesc("timespan").last("limit 1")).getTimespan();
            for (int i =0;i<s.size();i++){
                Long temp = Long.valueOf(s.get(i));
                if(temp%total==0){
                    if(weekday.contains(temp/total)){
                        time+=",第"+total+"节";
                    }
                    else{
                        if(weekday.size()==0){
                            time+= "周" + (temp / total) + "第" + total+"节";
                            weekday.add(temp/total);
                        }
                        else{
                            time+= ";周" + (temp / total) + "第" + total+"节";
                            weekday.add(temp/total);
                        }
                    }
                    continue;
                }
                int j;
                for (j=0;j<weekday.size();j++){
                    if((temp/total+1)==weekday.get(j)){
                        break;
                    }
                }
                if(j==weekday.size()){
                    weekday.add(temp/total+1);
                    if(weekday.size()==1) {
                        time += "周" + (1+temp / total) + "第" + temp % total+"节";
                    }
                    else{
                        time+= ";周" + (1+temp / total) + "第" + temp % total+"节";
                    }
                }
                else{
                    time+=",第"+temp%total+"节";
                }

            }
            u.setTime(time);

        });
        return Result.succ(pageData);
    }
}
